
package inventory;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.Timer;


/**
 *
 * @author Mark King
 */
public class MainUI extends javax.swing.JPanel {
    
    
    private Connection conn;
    private String name;
    
    private boolean verify = false;
    public boolean closeWindows=false;
    
    private int width = 861;
    private int height = 667;
    private int desktopScreenWidth;
    private int desktopScreenHeight;
    
    private JFrame customerFrame;
    private JFrame supplierFrame;
    private JFrame inventoryFrame;
    private JFrame installFrame;
    private JFrame orderFrame;
    private JFrame accountFrame;     
    
    public CustomerSectionUI customer;
    public SupplierSectionUI supplier;
    public InventorySectionUI inventory;
    public PreInstalledSectionUI install;
    public OrderSectionUI order;
    public AccountSectionUI account;
    
    /** Creates new form mainUI */
    public MainUI(Connection conn, String name, int screenWidth, int screenHeight) {
        this.conn = conn;
        this.name = name;
        this.desktopScreenWidth = screenWidth;
        this.desktopScreenHeight = screenHeight;
        initComponents();
        initialUpdate();
        customer = new CustomerSectionUI(conn, name, desktopScreenWidth, desktopScreenHeight);
        supplier = new SupplierSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        order = new OrderSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        install = new PreInstalledSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        inventory = new InventorySectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        customerFrame = new JFrame("Customer Section");
        supplierFrame = new JFrame("Supplier Section");
        orderFrame = new JFrame("Order Section");
        installFrame = new JFrame("Pre-Installed Section");
        inventoryFrame = new JFrame("Invnetory Section");
        accountFrame = new JFrame("Account Section");
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try{
                    if(customer.closeWindows || supplier.closeWindows || inventory.closeWindows || install.closeWindows || order.closeWindows || account.closeWindows){
                        closeWindows=true;
                        customer.closeWindows=true;
                        supplier.closeWindows=true;
                        inventory.closeWindows=true;
                        install.closeWindows=true;
                        order.closeWindows=true;
                        account.closeWindows=true;
                        try{
                            customerFrame.dispose();
                            supplierFrame.dispose();
                            inventoryFrame.dispose();
                            installFrame.dispose();
                            orderFrame.dispose();
                            accountFrame.dispose();
                        }catch(Exception e){}
                    }
                }catch(Exception e){}
            }
        };
        Timer timer = new Timer(0, actionListener);
        timer.start();
    }

    
    
    
    
    private void initialUpdate()
    {
        userLabel.setText(name);
    }
    
    public boolean getVerify()
    {
        return verify;
    }

    @Override
    public int getWidth()
    {
        return width;
    }
    @Override
    public int getHeight()
    {
        return height;
    }
    
    
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inventoryButton = new javax.swing.JButton();
        supplierButton = new javax.swing.JButton();
        preInstalledButton = new javax.swing.JButton();
        acccountButton = new javax.swing.JButton();
        orderButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        customerButton = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        userLabel = new javax.swing.JLabel();
        jbutton1 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        inventoryButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/inventory-maintenance-icon.png"))); // NOI18N
        inventoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inventoryButtonActionPerformed(evt);
            }
        });

        supplierButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/supplier.png"))); // NOI18N
        supplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supplierButtonActionPerformed(evt);
            }
        });

        preInstalledButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/install.png"))); // NOI18N
        preInstalledButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                preInstalledButtonActionPerformed(evt);
            }
        });

        acccountButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/user-icon.png"))); // NOI18N
        acccountButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acccountButtonActionPerformed(evt);
            }
        });

        orderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/order-history-icon.png"))); // NOI18N
        orderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orderButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Customer Section");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Supplier Section");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Account Details");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Inventory Section");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Order Section");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Pre-Installed Section");

        customerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/customerIcon.png"))); // NOI18N
        customerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customerButtonActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("User:");

        userLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        userLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        userLabel.setText("userText");

        jbutton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/logout-icon.png"))); // NOI18N
        jbutton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logOutButtonActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Log Out");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(310, 310, 310)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbutton1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(userLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(customerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(70, 70, 70)
                                .addComponent(inventoryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(acccountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(70, 70, 70)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(69, 69, 69)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                                .addComponent(jLabel6))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(supplierButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(69, 69, 69)
                                .addComponent(orderButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(59, 59, 59)
                                .addComponent(preInstalledButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(supplierButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(orderButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(preInstalledButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45)
                        .addComponent(jbutton1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(inventoryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(acccountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
        );
    }// </editor-fold>//GEN-END:initComponents

private void customerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customerButtonActionPerformed
        try{
            customerFrame.dispose();
        }
        catch(Exception e){}
        customerFrame = new JFrame("Customer Section");
        customer = new CustomerSectionUI(conn, name, desktopScreenWidth, desktopScreenHeight);
        customerFrame.add(customer);
        customerFrame.setSize(customer.getWidth(), customer.getHeight());
        customerFrame.setLocation((desktopScreenWidth -customer.getWidth()) /2, (desktopScreenHeight-customer.getHeight())/2);
        customerFrame.setResizable(false);
        customerFrame.setVisible(true);
}//GEN-LAST:event_customerButtonActionPerformed

private void supplierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supplierButtonActionPerformed
        try{
            supplierFrame.dispose();
        }
        catch(Exception e){}
        supplierFrame = new JFrame("Supplier Section");
        supplier = new SupplierSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        supplierFrame.add(supplier);
        supplierFrame.setSize(supplier.getWidth(), supplier.getHeight());
        supplierFrame.setLocation((desktopScreenWidth - supplier.getWidth()) /2, (desktopScreenHeight-supplier.getHeight())/2);
        supplierFrame.setResizable(false);
        supplierFrame.setVisible(true);      
}//GEN-LAST:event_supplierButtonActionPerformed

private void orderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orderButtonActionPerformed
        try{
           orderFrame.dispose();
        }
        catch(Exception e){}
        orderFrame = new JFrame("Order Section");
        order = new OrderSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        orderFrame.add(order);
        orderFrame.setSize(order.getWidth(), order.getHeight());
        orderFrame.setLocation((desktopScreenWidth - order.getWidth())/2, (desktopScreenHeight-order.getHeight())/2);
        orderFrame.setResizable(false);
        orderFrame.setVisible(true);
}//GEN-LAST:event_orderButtonActionPerformed

private void preInstalledButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preInstalledButtonActionPerformed
        try{
            installFrame.dispose();
        }
        catch(Exception e){}
        installFrame = new JFrame("Pre-Installed Section");
        install = new PreInstalledSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        installFrame.add(install);
        installFrame.setSize(install.getWidth(), install.getHeight());
        installFrame.setLocation((desktopScreenWidth -install.getWidth()) /2, (desktopScreenHeight-install.getHeight())/2);
        installFrame.setResizable(false);
        installFrame.setVisible(true);
}//GEN-LAST:event_preInstalledButtonActionPerformed

private void inventoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inventoryButtonActionPerformed
        try{
            inventoryFrame.dispose();
        }
        catch(Exception e){}
        inventoryFrame = new JFrame("Invnetory Section");
        inventory = new InventorySectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        inventoryFrame.add(inventory);
        inventoryFrame.setSize(inventory.getWidth(), inventory.getHeight());
        inventoryFrame.setLocation((desktopScreenWidth - inventory.getWidth()) /2, (desktopScreenHeight-inventory.getHeight())/2);
        inventoryFrame.setResizable(false);
        inventoryFrame.setVisible(true);
}//GEN-LAST:event_inventoryButtonActionPerformed

private void acccountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acccountButtonActionPerformed
        try{
            accountFrame.dispose();
        }
        catch(Exception e){}
        accountFrame = new JFrame("Account Section");
        account = new AccountSectionUI(conn, desktopScreenWidth, desktopScreenHeight);
        accountFrame.add(account);
        accountFrame.setSize(account.getWidth(), account.getHeight());
        accountFrame.setLocation((desktopScreenWidth - account.getWidth()) /2, (desktopScreenHeight - account.getHeight())/2);
        accountFrame.setResizable(false);
        accountFrame.setVisible(true);
}//GEN-LAST:event_acccountButtonActionPerformed

private void logOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logOutButtonActionPerformed
        try {
            conn.close();
            closeWindows=true;
            customer.closeWindows=true;
            supplier.closeWindows=true;
            inventory.closeWindows=true;
            install.closeWindows=true;
            order.closeWindows=true;
            account.closeWindows=true;
            try{
                order.customer.closeWindows=true;
                order.company.closeWindows=true;
            }catch(Exception e){}
            try{
                customerFrame.dispose();
                supplierFrame.dispose();
                inventoryFrame.dispose();
                installFrame.dispose();
                orderFrame.dispose();
                accountFrame.dispose();
            }catch(Exception e){}
        } catch (SQLException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_logOutButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acccountButton;
    private javax.swing.JButton customerButton;
    private javax.swing.JButton inventoryButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton jbutton1;
    private javax.swing.JButton orderButton;
    private javax.swing.JButton preInstalledButton;
    private javax.swing.JButton supplierButton;
    private javax.swing.JLabel userLabel;
    // End of variables declaration//GEN-END:variables
}
